﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using DevExpress.Xpf.NavBar;
using System.Windows.Media;

namespace BlueMonkey.View
{
    /// <summary>
    /// Interaction logic for xpender.xaml
    /// </summary>
    public partial class Xpender : UserControl
    {
        public Xpender()
        {
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(xpender_Loaded);
        }

        void xpender_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Draw();
        }

        private List<XpendeGroup> _xpenderGroup = new List<XpendeGroup>();

        public List<XpendeGroup> XpenderGroup
        {
            get { return _xpenderGroup; }
            set { _xpenderGroup = value; }
        }

        private bool Draw()
        {
            try
            {
                foreach (XpendeGroup group in _xpenderGroup)
                {
                    NavBarGroup g = new NavBarGroup() { Name = group.Name, Header = group.Header };
                    if (group.ImageSource != null)
                    {
                        g.ImageSource = group.ImageSource;
                    }
                    if ((group.ImageSource != null) && (group.ImageSettings != null))
                    {
                        g.ImageSettings = group.ImageSettings;
                    }
                    foreach (XpenderItem item in group.Items)
                    {
                        NavBarItem it = new NavBarItem();
                        it = item.Content;
                        g.Items.Add(it);
                    }
                    navBarControl.Groups.Add(g);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }

    public class XpendeGroup
    {
        public XpendeGroup() { }
        public XpendeGroup(string header) { this.Header = header; }
        public XpendeGroup(string header, string name) { this.Header = header; this.Name = name; }
        public ImageSource ImageSource;
        public ImageSettings ImageSettings;
        public string Name;
        
        public string Header;
        public object Tag;
        public List<XpenderItem> Items = new List<XpenderItem>();
    }

    public class XpenderItem    {
        public XpenderItem() { }
        public XpenderItem(object content) { Content.Content = content; }
        public XpenderItem(string content) { Content = xpenderItem(content); }
        public XpenderItem(string content, ImageSource image) { Content = xpenderItem(content, image); }
        public XpenderItem(string content, ImageSource image, int imgageWidth, int imageHeight) { Content = xpenderItem(content, image, imgageWidth, imageHeight); }

        public NavBarItem Content;

        private object _tag;
        public object Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        #region event
        public event EventHandler OnClick = delegate { };

        private void Click(object sender)
        {
            EventArgs argv = sender as EventArgs;
            OnClick(this.Content, argv);
        }
        #endregion

        public NavBarItem xpenderItem(string text)
        {
            Content = new NavBarItem();
            ImageSettings imgSetting = new ImageSettings() { Width = 32, Height = 32, Stretch = Stretch.Fill };
            LayoutSettings layout = new LayoutSettings() { ImageDocking = Dock.Right, ImageHorizontalAlignment = System.Windows.HorizontalAlignment.Right };
            Content.ImageSettings = imgSetting;
            Content.LayoutSettings = layout;
            Content.Content = text;
            Content.Click += new EventHandler(Content_Click);
            return Content;
        }

        public NavBarItem xpenderItem(string text, ImageSource image)
        {
            Content = new NavBarItem() { ImageSource = image };
            ImageSettings imgSetting = new ImageSettings() { Width = 32, Height = 32, Stretch = Stretch.Fill };
            LayoutSettings layout = new LayoutSettings() { ImageDocking = Dock.Right, ImageHorizontalAlignment = System.Windows.HorizontalAlignment.Right };
            Content.ImageSettings = imgSetting;
            Content.LayoutSettings = layout;
            Content.Content = text;
            Content.Click += new EventHandler(Content_Click);
            return Content;
        }

        public NavBarItem xpenderItem(string text, ImageSource image, int imgageWidth, int imageHeight)
        {
            Content = new NavBarItem() { ImageSource = image };
            ImageSettings imgSetting = new ImageSettings() { Width = imgageWidth, Height = imageHeight, Stretch = Stretch.Fill };
            LayoutSettings layout = new LayoutSettings() { ImageDocking = Dock.Right, ImageHorizontalAlignment = System.Windows.HorizontalAlignment.Right };
            Content.ImageSettings = imgSetting;
            Content.LayoutSettings = layout;
            Content.Content = text;
            Content.Click += new EventHandler(Content_Click);
            return Content;
        }

        void Content_Click(object sender, EventArgs e)
        {
            Click(sender);
        }
    }
}
